package cn.itcast.jx.action.cargo;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.ui.Model;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.common.SysConstant;
import cn.itcast.jx.domain.Contract;
import cn.itcast.jx.domain.User;
import cn.itcast.jx.poi.ContractPrint;
import cn.itcast.jx.service.ContractService;
import cn.itcast.jx.util.Page;

public class ContractAction extends BaseAction implements ModelDriven<Contract>{
	
	private ContractService contractService;

	private Contract model = new Contract();
	private String menuType = "contract";
	
	public String list() {
		String hql = "from Contract where 1=1 ";
		
		User user = (User) session.get(SysConstant.CURRENT_USER_INFO);
		if(user.getUserInfo().getDegree() == 4) {
			//普通员工
			hql += " and createBy='"+user.getId() + "'";
		}else if (user.getUserInfo().getDegree() == 3) {
			//部门经理
			hql += " and createDept = '" + user.getDept().getId() + "'";
		}else if(user.getUserInfo().getDegree() == 2) {
			//管理本部门和下属部门
			hql += "and createDept in()";
		}else if(user.getUserInfo().getDegree() == 2) {
			
		}else {
			//总裁
		}
		
		contractService.findPage(hql, page, Contract.class, null);
		page.setUrl("contractAction_list?cwhich=contract");
		push(page);
		return "list";
	}
	
	public String toview() {
		Contract contract = contractService.get(Contract.class, model.getId());
		push(contract);
		
		return "toview";
	}
	
	public String toupdate() {
		Contract contract = contractService.get(Contract.class, model.getId());
		String hql = "from Contract where state = 1";
		List<Contract> contractList = contractService.find(hql, Contract.class, null);
		
		put("contractList", contractList);
		
		push(contract);
		return "toupdate";
	}
	public String update() {
		contractService.saveOrUpdate(model);
		return "tolist";
	}
	public String tocreate() {
		String hql = "from Contract where state = 1";
		List<Contract> contractList = contractService.find(hql, Contract.class, null);
		put("contractList", contractList);
			
		return "tocreate";
	}
	public String insert() {
		
		User user = (User)session.get(SysConstant.CURRENT_USER_INFO);
		
		model.setCreateBy(user.getId());
		model.setCreateDept(user.getDept().getId());
		contractService.saveOrUpdate(model);
		
		return "tolist";
	}
	
	
	public String delete() {
		String[] ids = model.getId().split(", ");
		contractService.delete(Contract.class, ids);
		return "tolist";
	}

	public String submit() {
		if(model.getId() == null ) {
			return "tolist";
			
		}
		String[] ids = model.getId().split(", ");
		for (String id : ids) {
			Contract contract = contractService.get(Contract.class, id);
			contract.setState(2);
			contractService.saveOrUpdate(contract);
		}
		return "tolist";
	}
	
	public String cancel() {
		if(model.getId() == null ) {
			return "tolist";
			
		}
		String[] ids = model.getId().split(", ");
		for (String id : ids) {
			Contract contract = contractService.get(Contract.class, id);
			contract.setState(0);
			contractService.saveOrUpdate(contract);
		}
		return "tolist";
	}
	public String print() {
		Contract contract = contractService.get(Contract.class, model.getId());
		ContractPrint cp = new ContractPrint();
		try {
			cp.print(contract, ServletActionContext.getRequest().getRealPath("/"), ServletActionContext.getResponse()  );
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return NONE;
	}
	
	
	@Override
	public Contract getModel() {
		// TODO Auto-generated method stub
		return model;
	}
	
	public String getMenuType() {
		return menuType;
	}

	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}

	private Page page = new Page();
	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

}
